home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / visulztn / saoimage / saoimage.lha / readfith.c < prev    next >
C/C++ Source or Header  |  1990-04-20  |  6KB  |  189 lines

  1. #ifndef lint
  2. static char SccsId[] = "%W%  %G%";
  3. #endif
  4.  
  5. /* Module:    readfith.c (Read FITS Header)
  6.  * Purpose:    Parse FITS header for specific parameter fields
  7.  * Subroutine:    read_fitsheader()            returns: void
  8.  * Subroutine:    no_fitscomment()            returns: void
  9.  * Copyright:    1989 Smithsonian Astrophysical Observatory
  10.  *        You may do anything you like with this file except remove
  11.  *        this copyright.  The Smithsonian Astrophysical Observatory
  12.  *        makes no representations about the suitability of this
  13.  *        software for any purpose.  It is provided "as is" without
  14.  *        express or implied warranty.
  15.  * Modified:    {0} Michael VanHilst    initial version          23 January 1989
  16.  *        {1} Tim Cornwell (NRAO) not reject BITPIX < 0       1 Feb 1990
  17.  *              {1} MVH BSDonly strings.h compatability           19 Feb 1990
  18.  *        {n} <who> -- <does what> -- <when>
  19.  */
  20.  
  21. #include <stdio.h>        /* get stderr, NULL, etc. */
  22.  
  23. #ifndef VMS
  24. #ifdef SYSV
  25. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  26. #else
  27. #include <strings.h>        /* strlen, strcat, strcpy, rindex */
  28. #endif
  29. #else
  30. #include <string.h>        /* strlen, strcat, strcpy, strrchr */
  31. #endif
  32.  
  33.  
  34. /*
  35.  * Subroutine:    read_fitsheader
  36.  * Purpose:    Find important FITS parameters in FITS header
  37.  * Returns:    1 if success, else 0
  38.  */
  39. int read_fitsheader ( header, length, bitpix, naxis, naxes, scale, bias )
  40.      char *header;
  41.      int length;
  42.      int *bitpix;
  43.      int *naxis;
  44.      int *naxes;
  45.      float *scale;
  46.      float *bias;
  47. {
  48.   static int get_keyint(), get_keyfloat();
  49.  
  50.   /* first card is assumed to be SIMPLE = T, so skip it */
  51.   header += 80;
  52.   if( (*bitpix = get_keyint(header,"BITPIX  ", 80, 1)) == 0 )
  53.     return(0);
  54.   if( (*bitpix % 8) != 0 ) {
  55.     (void)fprintf(stderr,
  56.           "BITPIX = %d: not 8, 16, 32, -16, -32, or -64\n", *bitpix);
  57.     return(0);
  58.   }
  59.   header += 80;
  60.   if( (*naxis = get_keyint(header, "NAXIS   ", 80, 1)) <= 0 )
  61.     return(0);
  62.   header += 80;
  63.   if( (naxes[0] = get_keyint(header, "NAXIS1  ", 80, 1)) <= 0 )
  64.     return(0);
  65.   header += 80;
  66.   if( (naxes[1] = get_keyint(header, "NAXIS2  ", 80, 1)) <= 0 )
  67.     return(0);
  68.   header += 80;
  69.   length -= 400;
  70.   if( *naxis > 2 ) {
  71.     if( (naxes[2] = get_keyint(header, "NAXIS3  ", 80, 1)) <= 0 )
  72.       return(0);
  73.     header += 80;
  74.     length -= 80;
  75.   }
  76.   if( get_keyfloat(header, "BZERO   ", length, bias, 0) == 0 )
  77.     *bias = 0.0;
  78.   if( get_keyfloat(header, "BSCALE  ", length, scale, 0) == 0 )
  79.     *scale = 1.0;
  80.   return(1);
  81. }
  82.  
  83. /*
  84.  * Subroutine:    get_keyint
  85.  * Purpose:    Return the int value in the data field for a given FITS
  86.  *        header keyword.  If key not found, return 0.
  87.  */
  88. static int get_keyint ( header, keyword, length, report_error)
  89.      char *header;    /* buffer start */
  90.      char *keyword;    /* keyword to match */
  91.      int length;    /* if zero, search up to "END" keyword */
  92.      int report_error;    /* if > 0, fatal if key not found */
  93. {
  94.   int key_not_end, val;
  95.   int i;
  96.   void no_fitscomment();
  97.  
  98.   key_not_end = (strncmp(keyword, "END     ", 8) != 0);
  99.   for( i=0; i<length; i+=80 ) {
  100.     /* check for END keyword marking end of header, unless END is the key */
  101.     if( key_not_end && strncmp(header+i,"END     ",8) == 0 )
  102.       break;
  103.     /* check for desired keyword */
  104.     if( strncmp(header+i,keyword,8) == 0 ) {
  105.       no_fitscomment (header+i+10, 20);
  106.       if( (sscanf(header+i+10,"%d", &val) != 1) || (val == 0) ) {
  107.     (void)fprintf(stderr,
  108.               "Bad integer value for %s keyword in FITS header\n",
  109.               keyword);
  110.     return( 0 );
  111.       }
  112.       return( val );
  113.     }
  114.   }
  115.   if( report_error )
  116.     (void)fprintf(stderr, "No `%s' keyword in FITS header\n", keyword);
  117.   return( 0 );
  118. }
  119.  
  120. /*
  121.  * Subroutine:    get_keyfloat
  122.  * Purpose:    Return the float value in the data field for a given FITS
  123.  *        header keyword.  If key not found, return 0.
  124.  */
  125. static int get_keyfloat ( header, keyword, length, val, report_error)
  126.      char *header;    /* buffer start */
  127.      char *keyword;    /* keyword to match */
  128.      int length;    /* if zero, search up to "END" keyword */
  129.      float *val;    /* float to recieve value if found */
  130.      int report_error;    /* if > 0, fatal if key not found */
  131. {
  132.   int key_not_end;
  133.   int i;
  134.   static void fix_exponent();
  135.   void no_fitscomment();
  136.  
  137.   key_not_end = (strncmp(keyword, "END     ", 8) != 0);
  138.   for( i=0; i<length; i+=80  ) {
  139.     /* check for END keyword marking end of header, unless END is the key */
  140.     if( key_not_end && strncmp(header+i,"END     ",8) == 0 )
  141.       break;
  142.     /* check for desired keyword */
  143.     if( strncmp(header+i,keyword,8) == 0 ) {
  144.       no_fitscomment (header+i+10, 20);
  145.       fix_exponent (header+i+10, 20);
  146.       if( sscanf(header+i+10,"%f", val) != 1 ) {
  147.     (void)fprintf(stderr,
  148.               "Bad numerical value for %s keyword in FITS header\n",
  149.               keyword);
  150.     return( 0 );
  151.       }
  152.       return( 1 );
  153.     }
  154.   }
  155.   if( report_error )
  156.     (void)fprintf(stderr, "No `%s' keyword in FITS header\n", keyword);
  157.   return( 0 );
  158. }
  159.  
  160. /*
  161.  * Subroutine:    no_fitscomment
  162.  * Purpose:    Terminate the data field at an comment indicator.  The FITS
  163.  *        standard allows comments immediately after any data.
  164.  */
  165. void no_fitscomment ( line, count )
  166.      char *line;
  167.      int count;
  168. {
  169.   int i;
  170.   for( i=0; i<count; i++ )
  171.     if( line[i] == '/' ) line[i] = '\0';
  172. }
  173.  
  174. /*
  175.  * Subroutine:    fix_exponent
  176.  * Purpose:    Change misguided exponents using D to use E.  This problem
  177.  *        reportedly appears in output from some IRAF tasks.
  178.  */
  179. static void fix_exponent ( line, count )
  180.      char *line;
  181.      int count;
  182. {
  183.   int i;
  184.   for( i = 0; (i < count) && (line[i] != '\0'); i++ ) {
  185.     if( line[i] == 'D' ) line[i] = 'E';
  186.     if( line[i] == 'd' ) line[i] = 'e';
  187.   }
  188. }
  189.